package app.kotlin_1.sql

import app.kotlin_1.bean.NSearchBean
import app.kotlin_1.gen.NSearchBeanDao
import org.greenrobot.greendao.annotation.Property
import org.greenrobot.greendao.query.QueryBuilder
import org.greenrobot.greendao.query.WhereCondition

/**
 * Created by 刘智强 on 2017/9/27.
 * 数据库操作类
 */
class SearchDaoUtils {


    private var sqlManager: SQLiteManager? = null
    private var searchDao: NSearchBeanDao? = null

    constructor(sqlManager: SQLiteManager?) {
        this.sqlManager = sqlManager
        searchDao = sqlManager?.getDaoSession()?.getDao(NSearchBean::class.java) as NSearchBeanDao?
    }

    /**
     * 插入单条数据
     */
    fun insertBean(bean: NSearchBean?): Boolean {
        var flag: Boolean = false
        var index = sqlManager?.getDaoSession()?.nSearchBeanDao?.insert(bean)
        flag = index != -1L
        return flag
    }

    /**
     * 批量插入数据
     */
    fun insertBeans(beanList: MutableList<NSearchBean>?): Boolean {
        var flag: Boolean = false
        flag = try {
            kotlin.run {
                beanList?.forEach { sqlManager?.getDaoSession()?.insertOrReplace(beanList) }
                true
            }
        } catch (e: Exception) {
            false
        }

        return flag
    }

    /**
     * 修改单条数据
     */

    fun updateBean(bean: NSearchBean?): Boolean {

        var flag = false
        flag = try {
            sqlManager?.getDaoSession()?.update(bean)
            true
        } catch (e: Exception) {
            false

        }

        return flag


    }

    /**
     * 批量修改
     */
    fun updateBeans(beanList: MutableList<NSearchBean>?): Boolean {
        var flag = false
        flag = try {

            kotlin.run { beanList?.forEach { sqlManager?.getDaoSession()?.update(it) } }
            true
        } catch (e: Exception) {
            false
        }

        return flag

    }

    /**
     * 删除单条记录
     */

    fun deleteBean(bean: NSearchBean?): Boolean {

        var flag = false
        flag = try {
            sqlManager?.getDaoSession()?.delete(bean)
            true

        } catch (e: Exception) {
            false
        }

        return flag

    }

    /**
     * 批量删除
     */

    fun deleteBeans(beanList: MutableList<NSearchBean>): Boolean {
        var flag = false

        kotlin.run {

            flag = try {
                beanList?.forEach { sqlManager?.getDaoSession()?.delete(it) }
                true

            } catch (e: Exception) {
                false
            }
        }
        return flag

    }

    /**
     * 全部删除
     */
    fun deleteAll(): Boolean {
        var flag = false
        flag = try {
            sqlManager?.getDaoSession()?.deleteAll(NSearchBean::class.java)
            true

        } catch (e: Exception) {
            false
        }

        return flag

    }

    fun queryAll(): MutableList<NSearchBean> = searchDao?.loadAll()!!//查询所有

    fun queryById(id: Long): NSearchBean = searchDao?.load(id)!!// 根据ID查找

    fun queryBySql(sql: String, vararg c: String): MutableList<NSearchBean> = searchDao?.queryRaw(sql, c.toString())!!//根据sql语句和条件查询
    fun query(): QueryBuilder<NSearchBean>? = searchDao?.queryBuilder()?.orderDesc(NSearchBeanDao.Properties.Date)?.limit(5)//按照时间倒序前5条
    fun queryByName(str: String): QueryBuilder<NSearchBean>? = searchDao?.queryBuilder()?.where(WhereCondition.StringCondition("name like '%$str%' "))
}